// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
var app = angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'starter.config','jett.ionic.filter.bar'])
  .run(function ($rootScope,$ionicHistory,$state,APPCONSTANT,VersionService,TokenService) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, error) {
      if (toState.name == 'login') {
        $rootScope.navbarClass=true;
      } else {
        $rootScope.navbarClass=false;
      }
      if(toState.name == 'tab.userinfo'){
        TokenService.getLocalToken().then(function (data) {})
      }
    });
    VersionService.getVersionInfo(APPCONSTANT.deviceType).then(function (data) {
      if(data.version_count > APPCONSTANT.version_count){
        //todo:这里需要提示用户下载新的app
      }
    })

  })
.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})
  .config(function ($httpProvider, $ionicFilterBarConfigProvider) {
    //$httpProvider.interceptors.push('AuthInterceptor');
    $ionicFilterBarConfigProvider.placeholder('搜索');
  })
  .config(['$httpProvider', function ($httpProvider) {
    /*
     默认情况下通过CORS这样的方式是不会传递cookie.
     一般强制性将cookie添加到header的做法,也会被浏览器拒绝并报错.
     在服务器端会通过添加一个response头,Access-Control-Allow-Credentials来控制是否允许Cookie的提交.
     */
    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
  }])
.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('bottom');
    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
    $ionicConfigProvider.platform.android.navBar.alignTitle('center');
    $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
    $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
    $ionicConfigProvider.platform.ios.views.transition('ios');
    $ionicConfigProvider.platform.android.views.transition('android');

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

  .state('tab.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/home.html',
        controller: 'HomeCtrl'
      }
    }
  })

  .state('tab.cart', {
      url: '/cart',
      views: {
        'tab-cart': {
          templateUrl: 'templates/cart.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.information', {
      url: '/information',
      views: {
        'tab-information': {
          templateUrl: 'templates/information.html',
          controller: 'LoginCtrl'
        }
      }
    })

  .state('tab.userinfo', {
    url: '/userinfo',
    views: {
      'tab-userinfo': {
        templateUrl: 'templates/userinfo.html',
        controller: 'UserInfoCtrl'
      }
    }
  })
    //以上是一级页面


    //以下是商品详情页面

       /*商品详情*/
      .state('goodsDetail', {
        url: '/goodsDetail',
        templateUrl: 'templates/goods/goodsDetail.html',
        controller: 'HomeCtrl'
      })
      /*更多详情*/
      .state('goodsMoreDetail', {
        url: '/goodsMoreDetail',
        templateUrl: 'templates/goods/goodsMoreDetail.html',
        controller: 'GoodsDetailCtrl'
      })


    //以下是商品详情二级页面

      /*确认订单*/
      .state('checkBill', {
        url: '/checkBill',
        templateUrl: 'templates/goods/checkBill.html',
        controller: 'HomeCtrl'
      })


    //以下是商品详情三级页面

      /*支付*/
      .state('payBill', {
        url: '/payBill',
        templateUrl: 'templates/goods/payBill.html',
        controller: 'HomeCtrl'
      })
      /*发票信息*/
      .state('bill', {
        url: '/bill',
        templateUrl: 'templates/goods/bill.html',
        controller: 'HomeCtrl'
      })
      /*地址管理*/
      .state('address', {
        url: '/address',
        templateUrl: 'templates/goods/address.html',
        controller: 'AddressCtrl'
      })


    //以下是商品详情四级页面


      /*新增地址*/
      .state('addAddress', {
        url: '/addAddress',
        templateUrl: 'templates/goods/addAddress.html',
        controller: 'AddaddressCtrl'
      })


    //这以下是登录和注册那些页面
    .state('login', {
      url: '/login',
      templateUrl: 'templates/login/login.html',
      controller: 'LoginCtrl'
    })
    .state('findOldPassword', {
      url: '/findOldPassword',
      templateUrl: 'templates/login/findOldPassword.html',
      controller: 'LoginCtrl'
    })
    .state('changePassword', {
      url: '/changePassword',
      templateUrl: 'templates/login/changePassword.html',
      controller: 'LoginCtrl'
    })
    .state('registerUser', {
      url: '/registerUser',
      templateUrl: 'templates/login/registerUser.html',
      controller: 'LoginCtrl'
    })
    .state('selectCountry', {
      url: '/selectCountry',
      templateUrl: 'templates/login/selectCountry.html',
      controller: 'LoginCtrl'
    })
    //填写验证
    .state('confirmNumber', {
      url: '/confirmNumber',
      templateUrl: 'templates/login/confirmNumber.html',
      controller: 'LoginCtrl'
    })
      //服务条款
    .state('termsOfService',{
      url:'/termsOfService',
      templateUrl:'templates/login/termsOfService.html',
      controller:'LoginCtrl'
    })


     //这以下是首页的二级页面

      /*搜索*/
      .state('search', {
        url: '/search',
        templateUrl: 'templates/home/search.html',
        controller: 'HomeCtrl'
      })
      /*爱拼团*/
      .state('GroupBuy', {
        url: '/GroupBuy',
        templateUrl: 'templates/home/GroupBuy.html',
        controller: 'GroupBuyCtrl'
      })
      /*天天疯抢*/
      .state('secDeal', {
        url: '/secDeal',
        templateUrl: 'templates/home/secDeal.html',
        controller: 'ChatDetailCtrl'
      })
      /*值得买*/
      .state('worthBy', {
        url: '/worthBy',
        templateUrl: 'templates/home/worthBy.html',
        controller: 'ChatDetailCtrl'
      })
      /*品牌*/
      .state('brand', {
        url: '/brand',
        templateUrl: 'templates/home/brand.html',
        controller: 'HomeCtrl'
      })
      /*分类*/
      .state('divide', {
        url: '/divide',
        templateUrl: 'templates/home/divide.html',
        controller: 'HomeCtrl'
      })


    //这以下是首页的三级级页面

      /*更多推荐*/
      .state('recommend', {
        url: '/recommend/:moreType/:catId',
        templateUrl: 'templates/home/recommend.html',
        controller: 'RecommendCtrl'
      })
      /*店铺*/
      .state('store', {
        url: '/store',
        templateUrl: 'templates/home/store.html',
        controller: 'HomeCtrl'
      })
      /*查看全部*/
      .state('divideMore', {
        url: '/divideMore',
        templateUrl: 'templates/home/divideMore.html',
        controller: 'HomeCtrl'
      })


    //这以下是消息的二级页面

    /*系统公告*/
    .state('systemNotice', {
      url: '/systemNotice',
        templateUrl: 'templates/information/systemNotice.html',
        controller: 'ChatDetailCtrl'

    })


    //以下是个人中心的二级页面

      /*设置*/
      .state('setUserInfo', {
        url: '/setUserInfo',
        templateUrl: 'templates/set/setUserInfo.html',
        controller: 'UserInfoCtrl'
      })
      /*优惠券*/
      .state('coupon', {
        url: '/coupon',
        templateUrl: 'templates/set/coupon.html',
        controller: 'CouponCtrl'
      })
      /*优惠卡*/
      .state('card', {
        url: '/card',
        templateUrl: 'templates/set/card.html',
        controller: 'CouponCtrl'
      })
      /*订单列表*/
        .state('billList', {
        url: '/billList',
        templateUrl: 'templates/set/billList.html',
        controller: 'BillListCtrl'
      })


  //以下是个人中心的三级页面

      /*添加优惠券*/
      .state('addCoupon', {
        url: '/addCoupon',
        templateUrl: 'templates/set/addCoupon.html',
        controller: 'UserInfoCtrl'
      })
      /*账户安全*/
      .state('security', {
        url: '/security',
        templateUrl: 'templates/set/security.html',
        controller: 'UserInfoCtrl'
      })
      /*发表评价*/
      .state('evaluate', {
        url: '/evaluate',
        templateUrl: 'templates/set/evaluate.html',
        controller: 'UserInfoCtrl'
      })
      /*退货申请*/
      .state('return', {
        url: '/return',
        templateUrl: 'templates/set/return.html',
        controller: 'UserInfoCtrl'
      })
      /*实名认证*/
      .state('identification', {
        url: '/identification',
        templateUrl: 'templates/set/identification.html',
        controller: 'UserInfoCtrl'
      })
      /*物流信息*/
      .state('logistics', {
        url: '/logistics',
        templateUrl: 'templates/set/logistics.html',
        controller: 'UserInfoCtrl'
      })
      /*修改头像*/
    .state('updateuserimage', {
      url: '/updateuserimage',
      templateUrl: 'templates/set/updateuserimage.html',
      controller: 'UpdateUserImageCtrl'
    });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/home');

});
